package com.woniuxy.intellihome.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.intellihome.entity.ResponseEntity;
import com.woniuxy.intellihome.entity.ResponseState;
import com.woniuxy.intellihome.entity.User;
import com.woniuxy.intellihome.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jobob
 * @author ganrun 2021-8-12
 * @since 2021-08-11
 */
@RestController("admin/user")
@RequestMapping("admin/user")
public class UserController {
    @Autowired(required = true)
    private UserService userService;

    /**
     * 用户列表展示
     * 2021-8-12
     */
    @PostMapping("list")
    public ResponseEntity list(@RequestParam(value = "current", defaultValue = "1") Integer current,
                               @RequestParam(value = "pageSize", defaultValue = "2") Integer pageSize,
                               @RequestBody User user, BindingResult result) {

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (user.getAccount() != null && user.getAccount().trim().length() != 0) {
            queryWrapper.like("account", user.getAccount());
        }
        if (user.getStartRegTime() != null && user.getStartRegTime().trim().length() != 0) {
            queryWrapper.ge("reg_time", user.getStartRegTime());
        }
        if (user.getEndRegTime() != null && user.getEndRegTime().trim().length() != 0) {
            queryWrapper.le("reg_time", user.getEndRegTime());
        }
        if (user.getStartScore() != null) {
            queryWrapper.ge("score", user.getStartScore());
        }
        if (user.getEndScore() != null) {
            queryWrapper.le("score", user.getEndScore());
        }
        queryWrapper.eq("status", "y");


        Page<User> page = new Page<>(current, pageSize);

        if (result.hasFieldErrors()) {
            Map<String, String> msgs = new HashMap<>();
            result.getFieldErrors().forEach(e -> {
                msgs.put(e.getField(), e.getDefaultMessage());
            });
            ResponseEntity responseEntity = new ResponseEntity();
            responseEntity.setCode(ResponseState.INVALIDARGS);
            responseEntity.setMsg("无效参数");
            responseEntity.setData(msgs);
            return responseEntity;
        }
        Page<User> users = userService.page(page, queryWrapper);
        return new ResponseEntity(users);
    }

    /**
     * 注销用户(软删除)
     * 2021-8-12
     */
    @GetMapping("del/{id}")
    @ResponseBody
    public ResponseEntity del(@PathVariable int id) {
        User user = new User();
        user = userService.getById(id);
        user.setStatus("n");
        userService.saveOrUpdate(user);
        return ResponseEntity.SUCCESS;
    }
}

